package com.vq.controller;

import com.vq.common.R;
import com.vq.entity.CollectNote;
import com.vq.entity.Trends;
import com.vq.entity.User;
import com.vq.service.CollectNoteService;
import com.vq.service.FriendListService;
import com.vq.service.TrendsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/Trends")
public class TrendsController {
    private final HttpSession session;

    private final FriendListService friendListService;
    private final CollectNoteService collectNoteService;
    private final TrendsService trendsService;

    @Autowired
    public TrendsController(HttpSession session, FriendListService friendListService, CollectNoteService collectNoteService, TrendsService trendsService) {
        this.session = session;
        this.friendListService = friendListService;
        this.collectNoteService = collectNoteService;
        this.trendsService = trendsService;
    }

    /**
     * 发布动态
     * @param trends
     * @return
     */
    @PostMapping
    public R<Trends> publishTrend(@RequestBody Trends trends) {
        User user = (User) session.getAttribute("myUser");
        if(user == null) return new R<>(0);
        trends.setUsername(user.getUsername());
        trends.setAccount(user.getAccount());
        //进行转义
        //trends.setContent(Escape.getHTML(trends.getContent()));
        //添加日志
        log.info("发布动态："+trends);
        trendsService.deleteByIdInMongo(trends.getId());//删除草稿
        trendsService.insert(trends);
        return new R<Trends>(1, trends);
    }
    @PostMapping("/1")
    public R<?> aa(@RequestBody MultipartFile photo) throws IOException {
        System.out.println("1");
        //文件名
        //String name = photo.getName();
        //文件类型 content-type: "image/png",
        String type = photo.getContentType();
        //文件大小
        long size = photo.getSize();
        //eg: UUID + . + png
        String fileName = UUID.randomUUID() + "."
                + photo.getContentType().substring(
                photo.getContentType().lastIndexOf("/") + 1);    // 创建文件名称
        //文件路径  位置 + 文件名
        String filePath = "E:/" + fileName;
        //文件路径
        String path = filePath;
        File saveFile = new File(filePath);
        photo.transferTo(saveFile);        // 文件保存
        System.out.println("2");
        return new R<>(1);
    }



    /**
     * 删除自己的动态
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<?> deleteTrend(@PathVariable Long id) {
        User user = (User) session.getAttribute("myUser");
        if(user == null) return new R<>(0);
        trendsService.deleteByAccountAndId(user.getAccount(), id);
        return new R<>(1);
    }
    @DeleteMapping("/10")
    public R<?> deleteByIdInMongo(@RequestParam("id") Long id) {
        boolean bo = trendsService.deleteByIdInMongo(id);
        log.info("删除草稿："+id+","+bo);
        return new R<>(bo?1:0);
    }



    /**
     * 查询所有动态
     * @param limit 以查到条数
     * @return
     */
    @GetMapping("/1")
    public R<List<Trends>> selectAll(@RequestParam("limit") Integer limit) {
        List<Trends> trendsList = trendsService.selectLimit(limit);
        log.info("查询所有动态");
        return new R<>(1, trendsList);
    }
    /**
     * 查询指定动态
     * @param id 查询动态id
     * @return
     */
    @GetMapping()
    public R<Trends> selectAll(@RequestParam("id") Long id) {
        Trends trends = trendsService.selectById(id);
        log.info("查询id为"+id+"的动态");
        return new R<>(1, trends);
    }
    /**
     * 查询好友包括自己的动态
     * @return
     */
    @GetMapping("/2")
    public R<List<Trends>> selectFriendTrends() {
        User user = (User) session.getAttribute("myUser");
        if(user == null) return new R<>(0);
        List<User> users = friendListService.selectByMyAccount(user.getAccount());
        List<String> accounts = new ArrayList<>();
        accounts.add(user.getAccount());
        for (User user1 : users) {
            accounts.add(user1.getAccount());
        }
        List<Trends> trendsList = trendsService.selectByAccount(accounts);
        return new R<>(1, trendsList);
    }
    /**
     * 查询用户自己的动态
     * @return
     */
    @GetMapping("/3")
    public R<List<Trends>> selectMyTrends() {
        User user = (User) session.getAttribute("myUser");
        log.info("查询自己动态："+user);
        if (user == null) return new R<>(0);
        List<Trends> trendsList = trendsService.selectByAccount(user.getAccount());
        return new R<>(1, trendsList);
    }
    /**
     * 搜素动态
     * @param title
     * @return
     */
    @GetMapping("/4")
    public R searchTrends(@RequestParam String title) {
        List<Trends> trendsList = trendsService.searchTrends(title);
        return new R(1, trendsList);
    }
    /**
     * 查询具有该标签的动态
     * @param name
     * @return
     */
    @GetMapping("/5")
    public R selectByCommunityName(@RequestParam String name) {
        List<Trends> trendsList = trendsService.selectByCommunityName(name);
        return new R(1, trendsList);
    }
    /**
     * 查询收藏
     * @return
     */
    @GetMapping("/6")
    public R selectByCollect() {
        User myUser = (User) session.getAttribute("myUser");
        List<CollectNote> collectNoteList = collectNoteService.selectByAccount(myUser.getAccount());
        List<Trends> trendsList = new ArrayList<>();
        for (CollectNote collectNote : collectNoteList) {
            trendsList.add(trendsService.selectById(collectNote.getTrendsId()));
        }
        log.info("查询收藏动态："+trendsList);
        return new R(1, trendsList);
    }
    @GetMapping("/10")
    public R<?> selectByMeInMongo() {
        R<List<Trends>> r = new R<>(0);
        User myUser = (User) session.getAttribute("myUser");
        List<Trends> trendsList = trendsService.selectByMeInMongo(myUser.getAccount());
        r.setCode(1);
        r.setData(trendsList);
        return r;
    }
    /**
     * 在mongodb中查询动态
     * @param id
     * @return
     */
    @GetMapping("/11")
    public R<?> selectByIdInMongo(@RequestParam("id") Long id) {
        log.info("在mongodb查询草稿："+id);
        Trends trends = trendsService.selectByIdInMongo(id);
        return new R<>(trends!=null?1:0, trends);
    }



    /**
     * 保存草稿
     * @param trends
     * @return
     */
    @PutMapping()
    public R<?> save(@RequestBody Trends trends) {
        User myUser = (User) session.getAttribute("myUser");
        trends.setAccount(myUser.getAccount());
        Trends trends1 = trendsService.saveTrends(trends);
        return new R<>(trends1!=null?1:0, trends1);
    }
    /**
     * 点赞
     * @param id 要点赞的动态id
     * @return
     */
    @PutMapping("/1/{id}")
    public R<?> like(@PathVariable int id) {
        User user = (User) session.getAttribute("myUser");
        if (user==null) return new R<>(0);
        int bo = trendsService.like(user.getAccount(),id);
        return new R<>(bo);
    }
    /**
     * 收藏
     * @param id 要收藏的动态id
     * @return
     */
    @PutMapping("/2/{id}")
    @ResponseBody
    public R<?> collect(@PathVariable int id) {
        User user = (User) session.getAttribute("myUser");
        if (user==null) return new R<>(0);
        int bo = trendsService.collect(user.getAccount(),id);
        return new R<>(bo);
    }
}
